import {otherRouter, appRouter} from '@/router/router';
import Util from '@/libs/util';
import Cookies from 'js-cookie';
import Vue from 'vue';

const app = {
    state: {
        areaCachePage: [],
        lang: '',
        isFullScreen: false,
        openedSubmenuArr: [], // 要展开的菜单数组
        menuTheme: 'dark', // 主题
        themeColor: '',
        areaPageOpenedList: [{
            title: '首页',
            path: '',
            name: 'home_index'
        }],
        areaCurrentPageName: '',
        currentPath: [
            {
                title: '首页',
                path: '',
                name: 'home_index'
            }
        ], // 面包屑数组
        menuList: [],
        routers: [
            otherRouter,
            ...appRouter
        ],
        tagsList: [...otherRouter.children],
        messageCount: 0,
        dontCache: ['goods_add', 'goods-class-add', 'yc-goods-class-add', 'help_add', 'site-parameters-add', 'versions_add', 'create_ad', 'set_ad_detail', 'vipgrade_add', 'module-add', 'power_add', 'aptitude_add', 'company_user_add', 'role_add', 'brand_add', 'level_add', 'shop_add', 'order-info', 'module-edit', 'brand_edit', 'shop-edit', 'level_edit', 'goods-class-edit', 'yc-goods-class-edit', 'goods-edit', 'power_edit', 'role_edit', 'vip_edit', 'company_user_edit', 'aptitude_edit', 'site-parameters-edit', 'vipgrade_edit', 'help_edit', 'versions_edit', 'shopping_order_detail', 'refund_order_detail', 'shop_detail', 'join_detail', 'loan_detail', 'examine_detail', 'aptitude_detail', 'cloud_goods_detail', 'site-parameters-detail', 'financial-transactions-detail', 'transfer-accounts-detail'], // 在这里定义你不想要缓存的页面的name属性值(参见路由配置router.js)
    },
    mutations: {
        setTagsList(state, list) {
            state.tagsList.push(...list);
        },
        updateMenulist(state) {
            let accessCode = JSON.parse(sessionStorage.getItem('area_cloud_access')) || [];
            let menuList = [];
            let area_user_role_tag = parseInt(Cookies.get('area_user_role_tag'))
            if (area_user_role_tag === 1) {
                state.menuList = appRouter;
                return;
            }
            appRouter.forEach((item, index) => {
                let len = menuList.push(item);
                let childrenArr = [];
                childrenArr = item.children.filter(child => {
                    if (child.access !== undefined) {
                        if (accessCode.find(it => it.check_node === child.access)) {
                            return child;
                        }
                    } else {
                        return child;
                    }
                });
                if (childrenArr === undefined || childrenArr.length === 0) {
                    menuList.splice(len - 1, 1);
                } else {
                    let handledItem = JSON.parse(JSON.stringify(menuList[len - 1]));
                    handledItem.children = childrenArr;
                    menuList.splice(len - 1, 1, handledItem);
                }
            });
            state.menuList = menuList;
        },
        changeMenuTheme(state, theme) {
            state.menuTheme = theme;
        },
        changeMainTheme(state, mainTheme) {
            state.themeColor = mainTheme;
        },
        addOpenSubmenu(state, name) {
            let hasThisName = false;
            let isEmpty = false;
            if (name.length === 0) {
                isEmpty = true;
            }
            if (state.openedSubmenuArr.indexOf(name) > -1) {
                hasThisName = true;
            }
            if (!hasThisName && !isEmpty) {
                state.openedSubmenuArr.push(name);
            }
        },
        closePage(state, name) {
            state.areaCachePage.forEach((item, index) => {
                if (item === name) {
                    state.areaCachePage.splice(index, 1);
                }
            });
        },
        initCachepage(state) {
            if (localStorage.areaCachePage) {
                state.areaCachePage = JSON.parse(localStorage.areaCachePage);
            }
        },
        removeTag(state, name) {
            state.areaPageOpenedList.map((item, index) => {
                if (item.name === name) {
                    state.areaPageOpenedList.splice(index, 1);
                }
            });
        },
        areaPageOpenedList(state, get) {
            let openedPage = state.areaPageOpenedList[get.index];
            if (get.argu) {
                openedPage.argu = get.argu;
            }
            if (get.query) {
                openedPage.query = get.query;
            }
            state.areaPageOpenedList.splice(get.index, 1, openedPage);
            localStorage.areaPageOpenedList = JSON.stringify(state.areaPageOpenedList);
        },
        clearAllTags(state) {
            state.areaPageOpenedList.splice(1);
            state.areaCachePage.length = 0;
            localStorage.areaPageOpenedList = JSON.stringify(state.areaPageOpenedList);
        },
        clearOtherTags(state, vm) {
            let currentName = vm.$route.name;
            let currentIndex = 0;
            state.areaPageOpenedList.forEach((item, index) => {
                if (item.name === currentName) {
                    currentIndex = index;
                }
            });
            if (currentIndex === 0) {
                state.areaPageOpenedList.splice(1);
            } else {
                state.areaPageOpenedList.splice(currentIndex + 1);
                state.areaPageOpenedList.splice(1, currentIndex - 1);
            }
            let newCachepage = state.areaCachePage.filter(item => {
                return item === currentName;
            });
            state.areaCachePage = newCachepage;
            localStorage.areaPageOpenedList = JSON.stringify(state.areaPageOpenedList);
        },
        setOpenedList(state) {
            state.areaPageOpenedList = localStorage.areaPageOpenedList ? JSON.parse(localStorage.areaPageOpenedList) : [otherRouter.children[0]];
        },
        setCurrentPath(state, pathArr) {
            state.currentPath = pathArr;
        },
        setCurrentPageName(state, name) {
            state.areaCurrentPageName = name;
        },
        setAvator(state, path) {
            localStorage.areaAvatorImgPath = path;
        },
        switchLang(state, lang) {
            state.lang = lang;
            Vue.config.lang = lang;
        },
        clearOpenedSubmenu(state) {
            state.openedSubmenuArr.length = 0;
        },
        setMessageCount(state, count) {
            state.messageCount = count;
        },
        increateTag(state, tagObj) {
            if (!Util.oneOf(tagObj.name, state.dontCache)) {
                state.areaCachePage.push(tagObj.name);
                localStorage.areaCachePage = JSON.stringify(state.areaCachePage);
            }
            state.areaPageOpenedList.push(tagObj);
            localStorage.areaPageOpenedList = JSON.stringify(state.areaPageOpenedList);
        }
    }
};

export default app;
